package com.yuan.Controller.admin;


import com.yuan.Service.EmployeeService;
import com.yuan.constant.JwtClaimsConstant;
import com.yuan.dto.EmployeeDTO;
import com.yuan.dto.EmployeeLoginDTO;
import com.yuan.dto.EmployeePageQueryDTO;
import com.yuan.entity.Employee;
import com.yuan.properties.JwtProperties;
import com.yuan.result.PageResult;
import com.yuan.result.Result;
import com.yuan.utils.JwtUtil;
import com.yuan.vo.EmployeeLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/admin/employee")
@Tag(name = "员工相关接口")
@RequiredArgsConstructor
public class EmployeeController {

    private final EmployeeService employeeService;

    private final JwtProperties jwtProperties;

    @PostMapping("/login")
    @Operation(description = "员工登陆")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {

        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());

        String jwt = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = new EmployeeLoginVO(employee.getId(), employee.getUsername(), employee.getName(), jwt);

        return Result.success(employeeLoginVO);
    }

    @PostMapping("/logout")
    @Operation(description = "员工登出")
    public Result<String> logout() {

        return Result.success();
    }


    @PostMapping
    @Operation
    public Result save(@RequestBody EmployeeDTO employeeDTO) {

        log.info("新增员工：{}", employeeDTO);
        employeeService.saveSelf(employeeDTO);
        return Result.success();

    }

    //员工分页查询
    @GetMapping("/page")
    @Operation(description = "员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO dto) {
        log.info("员工分页查询，参数为：{}", dto);
        PageResult pageResult = employeeService.pageSelf(dto);
        return Result.success(pageResult);
    }

    //启用禁用员工账号
    @PostMapping("/status/{status}")
    @Operation( description = "启用禁用员工账号")
    public Result startOrStop(@PathVariable Integer status,Long id) {
        log.info("启用禁用员工账号：{},{}",status,id);
        employeeService.startOrStop(status,id);
        return Result.success();

    }

}
